package com.fenghuajueli.idiomppp.ui.main;

import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.MainTabPagerAdapter;
import com.fenghuajueli.idiomppp.databinding.ActivityMainBinding;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;
import com.fenghuajueli.libbasecoreui.widget.NoScrollViewPager;


import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends BaseActivity {

    RelativeLayout containerView;
    RadioButton rbTabHome;
    RadioButton rbTabPin;
    RadioButton rbTabShiYi;
    RadioButton rbTabSetting;
    NoScrollViewPager viewPager;
    RadioGroup radioGroup;
    MainTabPagerAdapter mainTabPagerAdapter;

    private ActivityMainBinding binding;

    private void initview() {
        containerView = binding.containerView;
        rbTabHome = binding.rbTabHome;
        rbTabPin = binding.rbTabPin;
        rbTabShiYi = binding.rbTabShiYi;
        rbTabSetting = binding.rbTabSetting;
        viewPager = binding.viewPager;
        radioGroup = binding.radioGroup;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initview();
        loadBg();
        mainTabPagerAdapter = new MainTabPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        viewPager.setAdapter(mainTabPagerAdapter);
        viewPager.setScrollable(false);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setCurrentItem(0, false);
        viewPager.addOnPageChangeListener(onPageChangeListener);
        rbTabHome.setOnCheckedChangeListener(onCheckedChangeListener);
        rbTabPin.setOnCheckedChangeListener(onCheckedChangeListener);
        rbTabSetting.setOnCheckedChangeListener(onCheckedChangeListener);
        rbTabShiYi.setOnCheckedChangeListener(onCheckedChangeListener);
    }


    private void loadBg() {
        ImageLoadManager.loadForViewGroup(containerView, R.mipmap.aa_szxl_bj);
    }

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }

        @Override
        public void onPageSelected(int i) {
            switch (i) {
                case 0:
                    rbTabHome.setOnCheckedChangeListener(null);
                    rbTabHome.setChecked(true);
                    rbTabHome.setOnCheckedChangeListener(onCheckedChangeListener);
                    break;
                case 1:
                    rbTabPin.setOnCheckedChangeListener(null);
                    rbTabPin.setChecked(true);
                    rbTabPin.setOnCheckedChangeListener(onCheckedChangeListener);
                    break;
                case 2:
                    rbTabShiYi.setOnCheckedChangeListener(null);
                    rbTabShiYi.setChecked(true);
                    rbTabShiYi.setOnCheckedChangeListener(onCheckedChangeListener);
                    break;
                case 3:
                    rbTabSetting.setOnCheckedChangeListener(null);
                    rbTabSetting.setChecked(true);
                    rbTabSetting.setOnCheckedChangeListener(onCheckedChangeListener);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }
    };

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                int id = buttonView.getId();
                if (id == R.id.rbTabHome) {
                    viewPager.setCurrentItem(0, false);

                } else if (id == R.id.rbTabPin) {
                    viewPager.setCurrentItem(1, false);
                } else if (id == R.id.rbTabShiYi) {
                    viewPager.setCurrentItem(2, false);
                } else if (id == R.id.rbTabSetting) {
                    viewPager.setCurrentItem(3, false);
                }
            }
        }
    };

    @Override
    public void onBackPressed() {
        exit();
    }

    private Boolean isQuit = false;

    public void exit() {
        if (isQuit == false) {
            Timer timerexit = new Timer();
            isQuit = true;
            ToastUtils.showShort("再按一次返回键回到桌面");
            TimerTask task = null;
            task = new TimerTask() {
                @Override
                public void run() {
                    isQuit = false;
                }
            };
            timerexit.schedule(task, 3000);
        } else {
            Intent backHome = new Intent(Intent.ACTION_MAIN);
            backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            backHome.addCategory(Intent.CATEGORY_HOME);
            startActivity(backHome);
        }
    }

}
